<template>
  <div class="detail">
    <header class="header">
      <ul class="header-flex">
        <li class="header-flex-left" @click="goBack">
          <img src="~/assets/imgs/back.png" alt="" />
        </li>
        <li class="header-flex-right">
          <p>文章详情</p>
        </li>
      </ul>
    </header>
    <section class="content-wrap">
      <template v-if="postInfo.postType === 2">
        <div id="videoRef"></div>
      </template>
      <div class="title">
        <h4>{{ postInfo.title }}</h4>
        <p class="time">{{ postInfo.updateTime | formatTime }}</p>
      </div>
      <template v-if="postInfo.postType === 1">
        <article class="article" v-html="postInfo.content"></article>
      </template>
    </section>
  </div>
</template>
<script>
import Player from 'xgplayer'

export default {
  async asyncData({ $axios, params }) {
    const result = await $axios.$get(`/app/queryPostDetail/${params.id}`)
    return { postInfo: result.data }
  },
  data() {
    return {
      postInfo: {},
    }
  },
  computed: {
    id() {
      return this.$route.params.id || ''
    },
  },
  mounted() {
    const screenWid = document.body.clientWidth
    if (this.postInfo.postType === 2) {
      // eslint-disable-next-line no-new
      new Player({
        id: 'videoRef',
        url: this.postInfo.videoUrl,
        width: screenWid,
        height: 210,
      })
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
  },
}
</script>
<style lang="scss" scoped>
.detail {
  width: 100%;
  font-size: 15px;
  line-height: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
  .header {
    position: fixed;
    width: 100%;
    top: 0;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    color: #fff;
    background: #666;
    z-index: 100;
    padding: 0 15px;
    &-flex {
      display: flex;
      &-left {
        flex: 0 0 32px;
        img {
          width: 20px;
          height: 20px;
          vertical-align: middle;
        }
      }
      &-right {
        flex: 1;
        text-align: center;
      }
    }
  }
  .content-wrap {
    width: 100%;
    padding: 48px 0;
    .title {
      padding: 12px 15px;
      h4 {
        line-height: 32px;
        font-size: 18px;
      }
      .time {
        font-size: 12px;
        color: #999;
        margin-top: 6px;
      }
    }
    .article {
      padding: 0 15px;
    }
  }
}
</style>
